package main

import (
	"flag"
	"gitee.com/jiewen/fsnotify"
	"log"
	"time"
)

var p string

func main() {
	flag.StringVar(&p, "p", "", "set path")
	flag.Parse()

	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer watcher.Close()

	go func() {
		for {
			select {
			case event, ok := <-watcher.Events:
				if !ok {
					return
				}
				log.Println("event:", event)
			case err, ok := <-watcher.Errors:
				if !ok {
					return
				}
				log.Println("error:", err)
			}
		}
	}()

	path := "/Users/qingwenjie/Downloads/test/fs"
	if p != "" {
		path = p
	}

	err = watcher.Add(path)
	if err != nil {
		log.Fatal(err)
	}

	for {
		if err := watcher.TriggerEvent(fsnotify.Trigger); err != nil {
			log.Fatal(err)
		}

		time.Sleep(1 * time.Minute)
	}
}
